﻿using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using Bing.Maps;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Mutzl.Samples.MapApp.Model;
using Mutzl.Samples.MapApp.View;
using Windows.Devices.Geolocation;
using System;

namespace Mutzl.Samples.MapApp.ViewModel
{
    public class MapViewModel : ViewModelBase
    {
        public MapViewModel() 
        {
            Zoom = 8;
            Center = new Location(47.7, 13.5);

            Finanzaemter = new ObservableCollection<Finanzamt>(Finanzamt.GetData());
            SonderObjekte = new ObservableCollection<SonderObjekt>(SonderObjekt.GetData());

            GoToDetailCommand = new RelayCommand<string>(GoToDetail);

            ZoomToFitCommand = new RelayCommand(ZoomToFit);

            CenterToCurrentLocationCommand = new RelayCommand(CenterToCurrentLocation);

            Messenger = new Messenger();
        }

        private async void CenterToCurrentLocation()
        {
            Center = await GetCurrentGeoLocationAsync();
            Zoom = 12;

            // Triggers the View, to close open Apenbars
            RaisePropertyChanged(() => HideAppBars);
        }

        private readonly Geolocator location = new Geolocator();
        private async Task<Location> GetCurrentGeoLocationAsync()
        {
            Geoposition position = await location.GetGeopositionAsync();
            return new Location(position.Coordinate.Latitude, position.Coordinate.Longitude);
        }

        private void ZoomToFit()
        {
            var locations = new LocationCollection();
            foreach (var finanzamt in Finanzaemter)
            {
                locations.Add(finanzamt.Location);
            }
            RaisePropertyChanged(() => HideAppBars);
            Messenger.Send(new NotificationMessage<LocationCollection>(locations, "ZoomToFit"));
        }

        private void GoToDetail(string parameter)
        {
            App.NavigationService.Navigate(typeof(DetailView), parameter);
        }

        private ObservableCollection<Finanzamt> _finanzaemter;
        public ObservableCollection<Finanzamt> Finanzaemter
        {
            get { return _finanzaemter; }
            set
            {
                if (_finanzaemter == value) return;
                _finanzaemter = value;
                RaisePropertyChanged(() => Finanzaemter);
            }
        }

        private ObservableCollection<SonderObjekt> _sonderObjekte;
        public ObservableCollection<SonderObjekt> SonderObjekte
        {
            get { return _sonderObjekte; }
            set
            {
                if (_sonderObjekte == value) return;
                _sonderObjekte = value;
                RaisePropertyChanged(() => SonderObjekte);
            }
        }

        private double _zoom;
        public double Zoom
        {
            get { return _zoom; }
            set
            {
                //if (_zoom == value) return;
                _zoom = value;
                RaisePropertyChanged(() => Zoom);
            }
        }

        private Location _center;
        public Location Center
        {
            get { return _center; }
            set
            {
                //if (_center == value) return;
                _center = value;
                RaisePropertyChanged(() => Center);
            }
        }

        public bool HideAppBars { get; private set; }

        public string MapsKey { get { return App.MapsKey; } }

        public RelayCommand<string> GoToDetailCommand { get; private set; }

        public ICommand ZoomToFitCommand { get; private set; }
        public ICommand CenterToCurrentLocationCommand { get; set; }

        public Messenger Messenger { get; private set; }

    }
}
